class Car {
  public constructor (public name: string) {
    
  }

  public drive (): void {
    console.log(`${this.name} Car is running`)
  }
}

/**
 * 简单工厂
 * ===> 一个工厂，根据参数，创建不同的对象，返回给调用者。
 * 
class AudiCar extends Car {
  public constructor () {
    super('Audi')
  }
}

class BydCar extends Car {
  public constructor () {
    super("BYD")
  }
}

class CarFactory {
  public static getCar(type: string): Car {
    let car: Car
    switch (type) {
      case 'Audi':
        car = new AudiCar()
        break
      case 'Byd':
        car = new BydCar()
        break
      default:
        throw new Error("Invalid car type")
    }
    return car
  }
}

*/


/* 
* 工厂模式 - 将具体的创建对象委派给具体的工厂

abstract class CarFactory {
  public abstract getCar (): Car 
}

class AudiCarFactory extends CarFactory {
  public getCar(): Car {
    let car: Car = new Car("Audi")
    return car
  }
}

class BydCarFactory extends CarFactory {
  public getCar(): Car {
    let car: Car = new Car("Byd")
    return car
  }
}

function main (): void {
  let car: Car = new AudiCarFactory().getCar()
  car.drive()
}

main()
*/

/** 抽象工厂
 *  一个工厂可以创建不同系列的对象
 */

interface CarFactory {
  createRaceCar (): Car
  createSuvCar (): Car
}

class AudiCarFactory implements CarFactory {
  createRaceCar (): Car {
    return new Car("Audi race")
  }

  createSuvCar (): Car {
    return new Car("Audi suv")
  }
}

class BydCarFactory implements CarFactory {
  createSuvCar (): Car {
    return new Car("Byd suv")
  }

  createRaceCar (): Car {
    return new Car("Byd race")
  }
}

function main_ () {
  const byd = new BydCarFactory()
  let car: Car = byd.createRaceCar()
  car.drive()
  car = byd.createSuvCar()
  car.drive()
}

main_()